# -*- coding: utf-8 -*-

'''eof
name:申请企业无历史信贷记录
code:EC61_EntNoCreditRecord
tableName:
columnName:
groups:企业征信
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:申请企业白户标签
eof'''

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

# 获取表信息方法
def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]


def EC61_EntNoCreditRecord():
    try:
        data = EC_DSJ_2D['data']
        flag = []
        # 信用提示信息
        EBA = getTable(data, 'EBA', 1, 1)
        if EBA == [{}]:
            return  u'缺失值'
        elif EBA == 0:
            return 1
        else:
            EB01 = getTable(EBA[0], 'EB01', 1, 0)
            if EB01 == 0:
                return 1
            elif EB01 == [{}]:
                return u'缺失值'
            else:
                #  信贷交易提示信息段
                EB01A = getTable(EB01[0], 'EB01A', 0, 1)
                if EB01A == 0:
                    return 1
                elif EB01A == [{}]:
                    return u'缺失值'
                else:
                    # 首次信贷年份
                    EB01AR01 = EB01A[0].get('EB01AR01')
                    if EB01AR01 is None or EB01AR01 == '':
                        return 1
                    else:
                        return 0
    except:
        return u"缺失值"


result = EC61_EntNoCreditRecord()

